#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

/*================================================
 Program: regs_cd_rf.do
 Author:  Avi Ebenstein
 Created: August 2007
 Purpose: OLS regressions of rainfall on death rates
          by share of households with tap water
=================================================*/

use ~/pollution/datafiles/dsp_basins.dta, replace;
 
gen digestivecancer_o=deathrate9;
gen dzcancer_a_esoph_o=deathrate_090;
gen dzcancer_b_stomach_o=deathrate_091;
gen dzcancer_c_liver_o=deathrate_095;
gen noncancer_o=deathrate_o-cancer_o;
gen lungcancer_o=deathrate10;
gen othercancer_o=cancer_o-deathrate9-deathrate10;

gen digestivecancer_m=maledr9;
gen dzcancer_a_esoph_m=maledr_090;
gen dzcancer_b_stomach_m=maledr_091;
gen dzcancer_c_liver_m=maledr_095;
gen noncancer_m=deathrate_m-cancer_m;
gen lungcancer_m=maledr10;
gen othercancer_m=cancer_m-maledr9-maledr10;

gen digestivecancer_f=femaledr9;
gen dzcancer_a_esoph_f=femaledr_090;
gen dzcancer_b_stomach_f=femaledr_091;
gen dzcancer_c_liver_f=femaledr_095;
gen noncancer_f=deathrate_f-cancer_f;
gen lungcancer_f=femaledr10;
gen othercancer_f=cancer_f-femaledr9-femaledr10;

gen tapshare=share_tap_water1>.50;
save ~/pollution/datafiles/temp_regs_cd_rf, replace;

capture program drop mytable;
program define mytable;
use ~/pollution/datafiles/temp_regs_cd_rf, clear;

****************************;
* List of control variables ;
****************************;

global controls "yrsed farmer urban lnairpollution i.region i.cla3";

****************************;
* List of chemicals         ;
****************************;

global mylist "overall_q a_n bod dissolved_ lead mercury oils permanga_n a_n volatile_p";
global mylist "rainfall";

local causes "deathrate cancer digestivecancer dzcancer_a_esoph dzcancer_b_stomach dzcancer_c_liver
lungcancer othercancer noncancer heart other respiratory stroke violent";

gen mypop_o=totalpop;
gen mypop_m=malepop;
gen mypop_f=femalepop;

gen str1 group="";;
global sexlist "o m f";

keep if tapshare==`1';

foreach k of global sexlist{;
replace group="`k'";                            
foreach j of local causes{;
                  gen lndeathrate_`j'_`k'=ln(`j'_`k');
foreach i of global mylist{;
                           preserve;
                           replace `i'=. if `i'==0;
                           xi: reg lndeathrate_`j'_`k' `i' $controls [w=mypop_`k'],cluster(provgb) robust;
                           gen beta=_b[`i'];
                           gen se=_se[`i'];
                           gen t=beta/se;
                           gen str cause="`j'";
                           gen str chemical="`i'";
                           keep cause chemical beta se t group;
                           keep in 1;
                           save ~/pollution/datafiles/run_`i'_`j'_`k', replace;
                           restore;
                         };
               };
                          };
clear;
set obs 1;
                 gen a=1;
foreach k of global sexlist{;                 
foreach j of local causes{;
foreach i of global mylist{;
                           append using ~/pollution/datafiles/run_`i'_`j'_`k';
                         };
               };
};

replace cause="a_deathrate" if cause=="deathrate";
replace cause="b_cancer" if cause=="cancer";
replace cause="c_digestivecancer" if cause=="digestivecancer";

replace cause="e_lungcancer" if cause=="lungcancer";
replace cause="e_othercancer" if cause=="othercancer";
replace cause="f_noncancer" if cause=="noncancer";
replace cause="g_heart" if cause=="heart";
replace cause="h_stroke" if cause=="stroke";
replace cause="i_respir" if cause=="respiratory";
replace cause="j_violent" if cause=="violent";
replace cause="k_other" if cause=="other";

replace chemical="a_a_overall_q" if chemical=="overall_q";

table cause group, c(mean beta mean se) f(%9.4fc) cellwidth(16);                 

end;

!rm ~/pollution/datafiles/run_*.dta;

pause on;
* No tap water - column 1;
mytable 0;
pause avi;
* Tap water - column 2;
mytable 1;

